<template>
    <div>
        <Row class="button_row">
                <span style="float: left;">
            <Button type="primary" @click="handleAdd()">录入</Button>
                    <!--<Button type="primary" class="step-item" style="margin-left:5px" size="small" @click="modal = true">调标配置查看</Button>-->
                </span>
        </Row>
        <Modal v-model="visible" :title="title" width="600" style="position:relative;">
            <Form ref="formData" :model="formData" :label-width="150" :rules="rules">
                <FormItem label="业务编码类型" prop="ywbm0001">
                    <dictSelect :sel_val='formData.ywbm0001' dictCode="codetype" :bindData.sync="formData.ywbm0001"
                                style="width: 300px"/>
                </FormItem>
                <FormItem label="截取区划编码位数" prop="ywbm0003">
                    <Input v-model="formData.ywbm0003" placeholder="" number style="width: 300px"/>
                </FormItem>
                <FormItem label="区划表转码字段" prop="ywbm0009">
                    <Input v-model="formData.ywbm0009" placeholder="" style="width: 300px"/>
                </FormItem>
                <FormItem label="前缀字符" prop="ywbm0002">
                    <Input v-model="formData.ywbm0002" placeholder="无前缀字符则不填写" style="width: 300px"/>
                </FormItem>
                <FormItem label="后缀字符" prop="ywbm0004">
                    <Input v-model="formData.ywbm0004" placeholder="无后缀字符则不填写" style="width: 300px"/>
                </FormItem>
                <FormItem label="流水号位数" prop="ywbm0005">
                    <Input v-model="formData.ywbm0005" placeholder="" number style="width: 300px"/>
                </FormItem>
                <FormItem label="数据表名" prop="ywbm0007">
                    <Input v-model="formData.ywbm0007" placeholder="" style="width: 300px"/>
                </FormItem>
                <FormItem label="数据字段" prop="ywbm0008">
                    <Input v-model="formData.ywbm0008" placeholder="" style="width: 300px"/>
                </FormItem>
                <FormItem label="数据表区划信息" prop="ywbm0010">
                    <Input v-model="formData.ywbm0010" placeholder="" style="width: 300px"/>
                </FormItem>
                <FormItem label="是否启用" prop="ywbm0006">
                    <dictSelect :sel_val='formData.ywbm0006' dictCode="yesorno" :bindData.sync="formData.ywbm0006"
                                style="width: 300px"/>
                </FormItem>
            </Form>
            <div slot="footer">
                <Button @click="visible = false">关闭</Button>
                <Button type="primary" @click="handleSubmit('formData')" v-if="showSubmit">确定</Button>
            </div>
        </Modal>
        <div class="table_div">
            <Table :columns="columns" :data="tableData" :loading="Loading">
                <template slot-scope="{ row, index  }" slot="action">
                    <Button type="info" size="small" style="margin-right: 5px" @click="handleEdit(index)">编辑</Button>
                    <Button type="success" size="small" style="margin-right: 5px" @click="handleView(index)">详情</Button>
                </template>
            </Table>
            <Page :total="dataCount" :page-size="pageSize" show-total class="paging"
                  @on-change="handleTableChange" :current="pageNo"></Page>
        </div>
    </div>
</template>

<script>
    import {postAction} from '@/api/request'
    import {CommonMixin} from '@/views/common/js/CommonMixin'
    import dictSelect from '@/components/dictSelect/dictSelect.vue'

    export default {
        mixins: [CommonMixin],
        components: {
            dictSelect
        },
        data() {
            return {
                rules: {
                    ywbm0001: [
                        {required: true, message: '信息必填', trigger: 'blur'}
                    ],
                    ywbm0003: [
                        {required: true, message: '必须是数字类型', trigger: 'blur', type: 'number'}
                    ],
                    ywbm0005: [
                        {required: true, message: '必须是数字类型', trigger: 'blur', type: 'number'}
                    ],
                    ywbm0007: [
                        {required: true, message: '信息必填', trigger: 'blur'},
                    ],
                    ywbm0008: [
                        {required: true, message: '信息必填', trigger: 'blur'},
                    ],
                    ywbm0006: [
                        {required: true, message: '信息必填', trigger: 'blur'}
                    ],
                    ywbm0010: [
                        {required: true, message: '信息必填', trigger: 'blur'}
                    ],
                },
                columns: [
                    {
                        title: '业务编码类型',
                        key: 'ywbm0001_name'
                    },
                    {
                        title: '截取区划编码位数',
                        key: 'ywbm0003'
                    },
                    {
                        title: '区划转码字段',
                        key: 'ywbm0009'
                    },
                    {
                        title: '前缀字符',
                        key: 'ywbm0002'
                    },
                    {
                        title: '后缀字符',
                        key: 'ywbm0004'
                    },
                    {
                        title: '流水号位数',
                        key: 'ywbm0005'
                    },
                    {
                        title: '数据表名',
                        key: 'ywbm0007'
                    },
                    {
                        title: '编码字段',
                        key: 'ywbm0008'
                    },
                    {
                        title: '数据表区划信息',
                        key: 'ywbm0010'
                    },
                    {
                        title: '是否启用',
                        key: 'ywbm0006_name'
                    },
                    {
                        title: '操作',
                        key: 'action',
                        align: 'center',
                        slot: 'action',
                        width: 200
                    }
                ],
                tableData: [],
                Loading: false,
                url: {
                    list: "/api/ac/subsistence/ss08001Service/getPageList",
                    edit: "/api/ac/subsistence/ss08001Service/updateData",
                    add: "/api/ac/subsistence/ss08001Service/insertData",
                    delete: "/api/ac/subsistence/ss08001Service/deleteData"
                },
                formData: {
                    ywbm0001: '',
                    ywbm0002: '',
                    ywbm0003: '',
                    ywbm0004: '',
                    ywbm0005: '',
                    ywbm0006: '',
                    ywbm0007: '',
                    ywbm0008: '',
                    ywbm0009: '',
                    ywbm0010: ''
                },
                queryParam: {},
                visible: false,
                showSubmit: true,
                dataCount: 0,
                pageSize: 10,
                modal: false,
                title: ""
            }
        },
        methods: {
            handleAdd() {
                let that = this;
                that.title = "业务编码规则配置录入"
                let record = {};
                that.visible = true
                that.showSubmit = true
                that.formData = Object.assign({}, record)
            },
            handleEdit(index) {
                let that = this;
                that.title = "业务编码规则配置编辑"
                let record = that.tableData[index];
                if (record.id) {
                    that.visible = true
                    that.showSubmit = true
                    that.formData = Object.assign({}, record)
                } else {
                    that.visible = false
                }
            },
            handleView(index) {
                let that = this;
                that.title = "业务编码规则配置查看"
                let record = that.tableData[index];
                if (record.id) {
                    that.visible = true
                    that.showSubmit = false
                    that.formData = Object.assign({}, record)
                } else {
                    that.visible = false
                }
            },
            handleSubmit(name) {
                this.$refs[name].validate((valid) => {
                    if (valid) {
                        let that = this;
                        let dataParam = {
                            id: that.formData.id,
                            ywbm0001: that.formData.ywbm0001,
                            ywbm0002: that.formData.ywbm0002,
                            ywbm0003: that.formData.ywbm0003,
                            ywbm0004: that.formData.ywbm0004,
                            ywbm0005: that.formData.ywbm0005,
                            ywbm0006: that.formData.ywbm0006,
                            ywbm0007: that.formData.ywbm0007,
                            ywbm0008: that.formData.ywbm0008,
                            ywbm0009: that.formData.ywbm0009,
                            ywbm0010: that.formData.ywbm0010,
                        }
                        let url = null;
                        if (dataParam.id == null) {
                            url = that.url.add
                        } else {
                            url = that.url.edit
                        }
                        postAction(url, dataParam).then((res) => {
                            if (res.data.data.code == 1) {
                                that.visible = false
                                this.$Message.success("提交成功！")
                                this.loadData();
                            } else {
                                this.$Message.error(res.data.data.msg);
                            }
                        }).finally(() => {

                        })
                    } else {
                        this.$Notice.error({title: '必填信息未填写完成!', desc: '必填信息未填写完成!'})
                    }
                });
            }
        },
        created() {
            this.loadData();
        },
    }
</script>
<style scoped>
    .paging {
        float: right;
        margin-top: 10px;
    }

    /deep/ .ivu-upload-list {
        display: none;
    }
</style>
